<template>
    <el-dialog v-model="show" title="编辑福利采购单" width="26%" align-center @close="handleClose">
        <el-row>
            <el-col class="flex" :span="24">
                <div class="label">
                    产品名称：
                </div>
                <div>{{ props.initList.welfareName }}</div>
            </el-col>
            <el-col class="flex" :span="12">
                <div class="label">
                    申请采购数量：
                </div>
                <div>{{ props.initList.applyNumber }}</div>
            </el-col>
            <el-col class="flex" :span="12">
                <div class="label">
                    申请日期：
                </div>
                <div>{{ props.initList.createTime }}</div>
            </el-col>
            <el-col class="flex" :span="24">
                <div class="label">
                    采购单价：
                </div>
                <el-input v-model="price" type="number" placeholder="请输入采购单价" />
            </el-col>
            <el-col class="flex" :span="24">
                <div class="label">
                    采购申请备注：
                </div>
                <div>{{ props.initList.remark }}</div>
            </el-col>
        </el-row>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="handleClose">取 消</el-button>
                <el-button type="primary" @click="handleSubmit()">保 存</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { editWelfareProcurementApi } from '@/api/sj/purchaseManagement/welfareProcurement/index'


const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const props = defineProps(['visible', 'initList']);
const emits = defineEmits(['submit', 'close']);

const show = ref(false);
const price = ref('')

watch(() => props.visible, async (newValue) => {
    show.value = newValue
    if (newValue) {
        if (props.initList.price) {
            price.value = props.initList.price
        } else {
            price.value = ''
        }
    }
}, { deep: true });

const handleSubmit = async () => {
    if (!price.value) return proxy?.$modal.msgError('请输入采购单价')

    let query: any = {};
    let nameList = Object.keys(props.initList);
    nameList.forEach(item => {
        if (props.initList[item] != null) {
            query[item] = props.initList[item];
        };
    });
    query.price = price.value;

    try {
        const { code } = await editWelfareProcurementApi(query)

        if (code == 200) {
            proxy?.$modal.msgSuccess('保存成功');
            emits('submit', true);
            handleClose();
        }
    } catch (error) {

    }
};


const handleClose = () => {
    emits('close');
};

onMounted(() => {
})
</script>

<style lang="scss" scoped>
.label {
    height: 32px;
    line-height: 32px;
    font-weight: 700;
    display: inline-flex;
    justify-content: flex-end;
    align-items: flex-start;
    padding: 0 12px 0 0;
    box-sizing: border-box;
    flex: 0 0 auto;
    font-size: var(--el-form-label-font-size);
    color: var(--el-text-color-regular);
    width: 130px;
}

.flex {
    display: flex;
    align-items: center;
}

.el-col {
    margin-bottom: 10px;
}
</style>